<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\CreateAbilityRequest;
use App\Http\Requests\UpdateAbilityRequest;
use App\Repositories\AbilityRepository;
use App\Http\Controllers\AppBaseController;
use Illuminate\Http\Request;
use Flash;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

class AbilityController extends AppBaseController
{
    /** @var  AbilityRepository */
    private $abilityRepository;

    public function __construct(AbilityRepository $abilityRepo)
    {
        $this->abilityRepository = $abilityRepo;
    }

    /**
     * Display a listing of the Ability.
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->abilityRepository->pushCriteria(new RequestCriteria($request));
        $abilities = $this->abilityRepository->all();

        return view('abilities.index')
            ->with('abilities', $abilities);
    }

    /**
     * Show the form for creating a new Ability.
     *
     * @return Response
     */
    public function create()
    {
        return view('abilities.create');
    }

    /**
     * Store a newly created Ability in storage.
     *
     * @param CreateAbilityRequest $request
     *
     * @return Response
     */
    public function store(CreateAbilityRequest $request)
    {
        $input = $request->all();

        $ability = $this->abilityRepository->create($input);

        Flash::success('Ability saved successfully.');

        return redirect(route('abilities.index'));
    }

    /**
     * Display the specified Ability.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $ability = $this->abilityRepository->findWithoutFail($id);

        if (empty($ability)) {
            Flash::error('Ability not found');

            return redirect(route('abilities.index'));
        }

        return view('abilities.show')->with('ability', $ability);
    }

    /**
     * Show the form for editing the specified Ability.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $ability = $this->abilityRepository->findWithoutFail($id);

        if (empty($ability)) {
            Flash::error('Ability not found');

            return redirect(route('abilities.index'));
        }

        return view('abilities.edit')->with('ability', $ability);
    }

    /**
     * Update the specified Ability in storage.
     *
     * @param  int              $id
     * @param UpdateAbilityRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateAbilityRequest $request)
    {
        $ability = $this->abilityRepository->findWithoutFail($id);

        if (empty($ability)) {
            Flash::error('Ability not found');

            return redirect(route('abilities.index'));
        }

        $ability = $this->abilityRepository->update($request->all(), $id);

        Flash::success('Ability updated successfully.');

        return redirect(route('abilities.index'));
    }

    /**
     * Remove the specified Ability from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $ability = $this->abilityRepository->findWithoutFail($id);

        if (empty($ability)) {
            Flash::error('Ability not found');

            return redirect(route('abilities.index'));
        }

        $this->abilityRepository->delete($id);

        Flash::success('Ability deleted successfully.');

        return redirect(route('abilities.index'));
    }
}
